﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml;
using System.Text;
using System.Windows.Forms;
using _7DaysToDieTools.Class;

namespace _7DaysToDieTools
{
    public partial class FormSconfig : Form
    {
        #region 变量定义
        private XmlDocument xmlDoc = null;
        private string tmpPath = "";
        #endregion

        #region 初始化
        public FormSconfig()
        {
            InitializeComponent();
        }

        public FormSconfig(Form pForm)
        {
            InitializeComponent();
            this.Location = new Point(pForm.Location.X + pForm.Width / 2 - this.Width / 2, pForm.Location.Y + pForm.Height / 2 - this.Height / 2);
        }

        private void FormSconfig_Load(object sender, EventArgs e)
        {
            tcMain.Enabled = false;
            保存ToolStripMenuItem.Visible = false;
            关闭ToolStripMenuItem.Visible = false;
        }

        private void FormSconfig_Shown(object sender, EventArgs e)
        {
            
        }
        #endregion

        #region 方法集

        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <param name="ConfigFile">配置文件名</param>
        /// <param name="xmlDoc">返回配置文件的xml实体</param>
        /// <returns></returns>
        private bool LoadConfig(string ConfigFile,out XmlDocument xmlDoc)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(ConfigFile);
                XmlElement xe = null;
                string tmp = "";

                //检测
                xe = xDoc.SelectSingleNode("/ServerSettings") as XmlElement;
                if (xe == null)
                {
                    xmlDoc = null;
                    MessageBox.Show("此文件不是七日杀的开服配置文件！请重新选择文件");
                    return false;
                }

                #region 服务器设置
                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='ServerIsPublic']") as XmlElement;
                tmp = xe.Attributes["value"].Value.ToLower();
                switch (tmp)
                {
                    case "true":
                        ckboxServerIsPublic.Checked = true;
                        break;
                    case "false":
                        ckboxServerIsPublic.Checked = false;
                        break;
                    default:
                        MessageBox.Show("'是否公开游戏'参数错误：" + tmp);
                        break;
                }
                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='EACEnabled']") as XmlElement;
                tmp = xe.Attributes["value"].Value.ToLower();
                switch (tmp)
                {
                    case "true":
                        ckboxEACEnabled.Checked = true;
                        break;
                    case "false":
                        ckboxEACEnabled.Checked = false;
                        break;
                    default:
                        MessageBox.Show("'启用EAC防作弊'参数错误：" + tmp);
                        break;
                }
                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='ServerName']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxServerName.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='ServerDescription']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxServerDesc.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='ServerPort']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxServerPort.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='ServerPassword']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxServerPassword.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='ServerMaxPlayerCount']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxServerMaxPlayerCount.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='ServerWebsiteURL']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxServerWebsiteURL.Text = tmp;
                #endregion

                #region 游戏设置
                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='GameWorld']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                switch (tmp)
                {
                    case "Navezgane":
                        cboxGameWorld.SelectedIndex = 0;
                        break;
                    case "Random Gen":
                        cboxGameWorld.SelectedIndex = 1;
                        break;
                    default:
                        MessageBox.Show("'游戏地图'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='GameMode']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                switch (tmp)
                {
                    case "GameModeSurvivalMP":
                        cboxGameMode.SelectedIndex = 0;
                        break;
                    case "GameModeSurvivalSP":
                        cboxGameMode.SelectedIndex = 1;
                        break;
                    default:
                        MessageBox.Show("'游戏模式'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='GameDifficulty']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                switch (tmp)
                {
                    case "0":
                        cboxGameDifficulty.SelectedIndex = 0;
                        break;
                    case "1":
                        cboxGameDifficulty.SelectedIndex = 1;
                        break;
                    case "2":
                        cboxGameDifficulty.SelectedIndex = 2;
                        break;
                    case "3":
                        cboxGameDifficulty.SelectedIndex = 3;
                        break;
                    case "4":
                        cboxGameDifficulty.SelectedIndex = 4;
                        break;
                    case "5":
                        cboxGameDifficulty.SelectedIndex = 5;
                        break;
                    default:
                        MessageBox.Show("'游戏难度'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='PlayerKillingMode']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                switch (tmp)
                {
                    case "0":
                        cboxPlayerKillingMode.SelectedIndex = 0;
                        break;
                    case "1":
                        cboxPlayerKillingMode.SelectedIndex = 1;
                        break;
                    case "2":
                        cboxPlayerKillingMode.SelectedIndex = 2;
                        break;
                    case "3":
                        cboxPlayerKillingMode.SelectedIndex = 3;
                        break;
                    default:
                        MessageBox.Show("'攻击模式'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='PersistentPlayerProfiles']") as XmlElement;
                tmp = xe.Attributes["value"].Value.ToLower();
                switch (tmp)
                {
                    case "true":
                        ckboxPersistentPlayerProfiles.Checked = true;
                        break;
                    case "false":
                        ckboxPersistentPlayerProfiles.Checked = false;
                        break;
                    default:
                        MessageBox.Show("'玩家档案不变'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='BuildCreate']") as XmlElement;
                tmp = xe.Attributes["value"].Value.ToLower();
                switch (tmp)
                {
                    case "true":
                        ckboxBuildCreate.Checked = true;
                        break;
                    case "false":
                        ckboxBuildCreate.Checked = false;
                        break;
                    default:
                        MessageBox.Show("'作弊模式'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='GameName']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxGameName.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='DayNightLength']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxDayNightLength.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='DayLightLength']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxDayLightLength.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='LootAbundance']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxLootAbundance.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='LootRespawnDays']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxLootRespawnDays.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='BlockDurabilityModifier']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxBlockDurabilityModifier.Text = tmp;

                
                #endregion

                #region 领地设置
                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimSize']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxLandClaimSize.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimDeadZone']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxLandClaimDeadZone.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimExpiryTime']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxLandClaimExpiryTime.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimDecayMode']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                switch (tmp)
                {
                    case "0":
                        cboxLandClaimDecayMode.SelectedIndex = 0;
                        break;
                    case "1":
                        cboxLandClaimDecayMode.SelectedIndex = 1;
                        break;
                    case "2":
                        cboxLandClaimDecayMode.SelectedIndex = 2;
                        break;
                    default:
                        MessageBox.Show("'宣告衰退格式'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimOnlineDurabilityModifier']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxLandClaimOnlineDurabilityModifier.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimOfflineDurabilityModifier']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxLandClaimOfflineDurabilityModifier.Text = tmp;
                #endregion

                #region 僵尸设置
                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='ZombiesRun']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                switch (tmp)
                {
                    case "0":
                        cboxZombiesRun.SelectedIndex = 0;
                        break;
                    case "1":
                        cboxZombiesRun.SelectedIndex = 1;
                        break;
                    case "2":
                        cboxZombiesRun.SelectedIndex = 2;
                        break;
                    default:
                        MessageBox.Show("'僵尸行动模式'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='MaxSpawnedZombies']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxMaxSpawnedZombies.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='MaxSpawnedAnimals']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxMaxSpawnedAnimals.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='EnemySenseMemory']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxEnemySenseMemory.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='EnemyDifficulty']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                switch (tmp)
                {
                    case "0":
                        cboxEnemyDifficulty.SelectedIndex = 0;
                        break;
                    case "1":
                        cboxEnemyDifficulty.SelectedIndex = 1;
                        break;
                    default:
                        MessageBox.Show("'敌人侵略性'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='EnemySpawnMode']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                switch (tmp)
                {
                    case "0":
                        cboxEnemySpawnMode.SelectedIndex = 0;
                        break;
                    case "1":
                        cboxEnemySpawnMode.SelectedIndex = 1;
                        break;
                    case "2":
                        cboxEnemySpawnMode.SelectedIndex = 2;
                        break;
                    case "3":
                        cboxEnemySpawnMode.SelectedIndex = 3;
                        break;
                    case "4":
                        cboxEnemySpawnMode.SelectedIndex = 4;
                        break;
                    case "5":
                        cboxEnemySpawnMode.SelectedIndex = 5;
                        break;
                    default:
                        MessageBox.Show("'敌人刷新强度'项参数错误：" + tmp);
                        break;
                }
                #endregion

                #region 后台设置
                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='ControlPanelEnabled']") as XmlElement;
                tmp = xe.Attributes["value"].Value.ToLower();
                switch (tmp)
                {
                    case "true":
                        ckboxControlPanelEnabled.Checked = true;
                        break;
                    case "false":
                        ckboxControlPanelEnabled.Checked = false;
                        break;
                    default:
                        MessageBox.Show("'网页控制台'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='ControlPanelPort']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxControlPanelPort.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='ControlPanelPassword']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxControlPanelPassword.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='TelnetEnabled']") as XmlElement;
                tmp = xe.Attributes["value"].Value.ToLower();
                switch (tmp)
                {
                    case "true":
                        ckboxTelnetEnabled.Checked = true;
                        break;
                    case "false":
                        ckboxTelnetEnabled.Checked = false;
                        break;
                    default:
                        MessageBox.Show("'Telnet控制台'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='TelnetPort']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxTelnetPort.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='TelnetPassword']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxTelnetPassword.Text = tmp;
                #endregion

                #region 其它设置
                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='AirDropFrequency']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxAirDropFrequency.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='AirDropMarker']") as XmlElement;
                tmp = xe.Attributes["value"].Value.ToLower();
                switch (tmp)
                {
                    case "true":
                        ckboxAirDropMarker.Checked = true;
                        break;
                    case "false":
                        ckboxAirDropMarker.Checked = false;
                        break;
                    default:
                        MessageBox.Show("'空投标记'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='DropOnDeath']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                switch (tmp)
                {
                    case "0":
                        cboxDropOnDeath.SelectedIndex = 0;
                        break;
                    case "1":
                        cboxDropOnDeath.SelectedIndex = 1;
                        break;
                    case "2":
                        cboxDropOnDeath.SelectedIndex = 2;
                        break;
                    case "3":
                        cboxDropOnDeath.SelectedIndex = 3;
                        break;
                    default:
                        MessageBox.Show("'死亡掉落'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='DropOnQuit']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                switch (tmp)
                {
                    case "0":
                        cboxDropOnQuit.SelectedIndex = 0;
                        break;
                    case "1":
                        cboxDropOnQuit.SelectedIndex = 1;
                        break;
                    case "2":
                        cboxDropOnQuit.SelectedIndex = 2;
                        break;
                    case "3":
                        cboxDropOnQuit.SelectedIndex = 3;
                        break;
                    default:
                        MessageBox.Show("'退出掉落'项参数错误：" + tmp);
                        break;
                }

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='AdminFileName']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxAdminFileName.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='PlayerSafeZoneLevel']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxPlayerSafeZoneLevel.Text = tmp;

                xe = xDoc.SelectSingleNode("/ServerSettings/property[@name='PlayerSafeZoneHours']") as XmlElement;
                tmp = xe.Attributes["value"].Value;
                tboxPlayerSafeZoneHours.Text = tmp;
                #endregion

                xe = null;
                xmlDoc = xDoc;
                return true;
            }
            catch(Exception x)
            {
                MessageBox.Show(x.Message);
                xmlDoc = null;
                return false;
            }
        }
        /// <summary>
        /// 保存配置文件
        /// </summary>
        /// <param name="ConfigFile">配置文件路径</param>
        /// <returns></returns>
        private bool SaveConfig(string ConfigFile)
        {
            try
            {
                XmlElement xe = null;
                #region 服务器设置
                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='ServerIsPublic']") as XmlElement;
                xe.Attributes["value"].Value = ckboxServerIsPublic.Checked.ToString().ToLower();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='EACEnabled']") as XmlElement;
                xe.Attributes["value"].Value = ckboxEACEnabled.Checked.ToString().ToLower();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='ServerName']") as XmlElement;
                xe.Attributes["value"].Value = tboxServerName.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='ServerDescription']") as XmlElement;
                xe.Attributes["value"].Value = tboxServerDesc.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='ServerPort']") as XmlElement;
                xe.Attributes["value"].Value = tboxServerPort.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='ServerPassword']") as XmlElement;
                xe.Attributes["value"].Value = tboxServerPassword.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='ServerMaxPlayerCount']") as XmlElement;
                xe.Attributes["value"].Value = tboxServerMaxPlayerCount.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='ServerWebsiteURL']") as XmlElement;
                xe.Attributes["value"].Value = tboxServerWebsiteURL.Text;
                #endregion

                #region 游戏设置
                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='GameWorld']") as XmlElement;
                switch (cboxGameWorld.SelectedIndex)
                {
                    case 0:
                        xe.Attributes["value"].Value = "Navezgane";
                        break;
                    case 1:
                        xe.Attributes["value"].Value = "Random Gen";
                        break;
                }

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='GameMode']") as XmlElement;
                switch (cboxGameMode.SelectedIndex)
                {
                    case 0:
                        xe.Attributes["value"].Value = "GameModeSurvivalMP";
                        break;
                    case 1:
                        xe.Attributes["value"].Value = "GameModeSurvivalSP";
                        break;
                }

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='GameDifficulty']") as XmlElement;
                xe.Attributes["value"].Value = cboxGameDifficulty.SelectedIndex.ToString();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='PlayerKillingMode']") as XmlElement;
                xe.Attributes["value"].Value = cboxPlayerKillingMode.SelectedIndex.ToString();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='PersistentPlayerProfiles']") as XmlElement;
                xe.Attributes["value"].Value = ckboxPersistentPlayerProfiles.Checked.ToString().ToLower();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='BuildCreate']") as XmlElement;
                xe.Attributes["value"].Value = ckboxBuildCreate.Checked.ToString().ToLower();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='GameName']") as XmlElement;
                xe.Attributes["value"].Value = tboxGameName.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='DayNightLength']") as XmlElement;
                xe.Attributes["value"].Value = tboxDayNightLength.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='DayLightLength']") as XmlElement;
                xe.Attributes["value"].Value = tboxDayLightLength.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='LootAbundance']") as XmlElement;
                xe.Attributes["value"].Value = tboxLootAbundance.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='LootRespawnDays']") as XmlElement;
                xe.Attributes["value"].Value = tboxLootRespawnDays.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='BlockDurabilityModifier']") as XmlElement;
                xe.Attributes["value"].Value = tboxBlockDurabilityModifier.Text;


                #endregion

                #region 领地设置
                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimSize']") as XmlElement;
                xe.Attributes["value"].Value = tboxLandClaimSize.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimDeadZone']") as XmlElement;
                xe.Attributes["value"].Value = tboxLandClaimDeadZone.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimExpiryTime']") as XmlElement;
                xe.Attributes["value"].Value = tboxLandClaimExpiryTime.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimDecayMode']") as XmlElement;
                xe.Attributes["value"].Value = cboxLandClaimDecayMode.SelectedIndex.ToString();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimOnlineDurabilityModifier']") as XmlElement;
                xe.Attributes["value"].Value = tboxLandClaimOnlineDurabilityModifier.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='LandClaimOfflineDurabilityModifier']") as XmlElement;
                xe.Attributes["value"].Value = tboxLandClaimOfflineDurabilityModifier.Text;
                #endregion

                #region 僵尸设置
                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='ZombiesRun']") as XmlElement;
                xe.Attributes["value"].Value = cboxZombiesRun.SelectedIndex.ToString();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='MaxSpawnedZombies']") as XmlElement;
                xe.Attributes["value"].Value = tboxMaxSpawnedZombies.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='MaxSpawnedAnimals']") as XmlElement;
                xe.Attributes["value"].Value = tboxMaxSpawnedAnimals.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='EnemySenseMemory']") as XmlElement;
                xe.Attributes["value"].Value = tboxEnemySenseMemory.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='EnemyDifficulty']") as XmlElement;
                xe.Attributes["value"].Value = cboxEnemyDifficulty.SelectedIndex.ToString();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='EnemySpawnMode']") as XmlElement;
                xe.Attributes["value"].Value = cboxEnemySpawnMode.SelectedIndex.ToString();
                #endregion

                #region 后台设置
                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='ControlPanelEnabled']") as XmlElement;
                xe.Attributes["value"].Value = ckboxControlPanelEnabled.Checked.ToString().ToLower();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='ControlPanelPort']") as XmlElement;
                xe.Attributes["value"].Value = tboxControlPanelPort.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='ControlPanelPassword']") as XmlElement;
                xe.Attributes["value"].Value = tboxControlPanelPassword.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='TelnetEnabled']") as XmlElement;
                xe.Attributes["value"].Value = ckboxTelnetEnabled.Checked.ToString().ToLower();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='TelnetPort']") as XmlElement;
                xe.Attributes["value"].Value = tboxTelnetPort.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='TelnetPassword']") as XmlElement;
                xe.Attributes["value"].Value = tboxTelnetPassword.Text;
                #endregion

                #region 其它设置
                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='AirDropFrequency']") as XmlElement;
                xe.Attributes["value"].Value = tboxAirDropFrequency.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='AirDropMarker']") as XmlElement;
                xe.Attributes["value"].Value = ckboxAirDropMarker.Checked.ToString().ToLower();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='DropOnDeath']") as XmlElement;
                xe.Attributes["value"].Value = cboxDropOnDeath.SelectedIndex.ToString();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='DropOnQuit']") as XmlElement;
                xe.Attributes["value"].Value = cboxDropOnQuit.SelectedIndex.ToString();

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='AdminFileName']") as XmlElement;
                xe.Attributes["value"].Value = tboxAdminFileName.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='PlayerSafeZoneLevel']") as XmlElement;
                xe.Attributes["value"].Value = tboxPlayerSafeZoneLevel.Text;

                xe = xmlDoc.SelectSingleNode("/ServerSettings/property[@name='PlayerSafeZoneHours']") as XmlElement;
                xe.Attributes["value"].Value = tboxPlayerSafeZoneHours.Text;
                #endregion
                XmlTextWriter xw = new XmlTextWriter(ConfigFile, new UTF8Encoding(false));
                xw.Formatting = Formatting.Indented;
                xmlDoc.Save(xw);
                xw.Close();
                return true;
            }
            catch(Exception x)
            {
                MessageBox.Show(x.Message);
                return false;
            }
        }
        #endregion

        #region 菜单
        private void 加载默认配置文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            xmlDoc = null;
            if (LoadConfig(Sinfo.PathCurr + Sinfo.File.ServerConfig, out xmlDoc))
            {
                tmpPath = Sinfo.PathCurr + Sinfo.File.ServerConfig;
                tcMain.Enabled = true;
                配置文件ToolStripMenuItem.Visible = false;
                保存ToolStripMenuItem.Visible = true;
                关闭ToolStripMenuItem.Visible = true;
            }
        }

        private void 加载指定配置文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            xmlDoc = null;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "配置文件|*.xml";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (LoadConfig(ofd.FileName, out xmlDoc))
                {
                    tmpPath = ofd.FileName;
                    tcMain.Enabled = true;
                    配置文件ToolStripMenuItem.Visible = false;
                    保存ToolStripMenuItem.Visible = true;
                    关闭ToolStripMenuItem.Visible = true;
                }
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tmpPath))
            {
                MessageBox.Show("配置文件保存路径出错，重新打开配置文件再修改保存吧！");
                关闭ToolStripMenuItem_Click(null, null);
                return;
            }
            if (SaveConfig(tmpPath))
            {
                tcMain.Enabled = false;
                配置文件ToolStripMenuItem.Visible = true;
                保存ToolStripMenuItem.Visible = false;
                关闭ToolStripMenuItem.Visible = false;
                MessageBox.Show("保存成功", "保存");
            }
            else
            {
                MessageBox.Show("保存失败！", "错误");
            }
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            xmlDoc = null;
            tcMain.Enabled = false;
            配置文件ToolStripMenuItem.Visible = true;
            保存ToolStripMenuItem.Visible = false;
            关闭ToolStripMenuItem.Visible = false;
        }
        #endregion
    }
}
